﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DH.Core.Helper
{
   public static class DateTimeHelper
    {
        public static DateTime DateOf1970_01_01 = new DateTime(1970, 1, 1);
        static TimeZoneInfo _ChinaStandardTimeTimeZone;
        public static TimeZoneInfo GetChinaStandardTimeTimeZone()
        {
            if (null == _ChinaStandardTimeTimeZone)
            {
                TimeZoneInfo timezone = TimeZoneInfo.FindSystemTimeZoneById("China Standard Time");
                _ChinaStandardTimeTimeZone = timezone;
            }

            return _ChinaStandardTimeTimeZone;
        }

        /// <summary>
        /// 获取北京标准时间
        /// </summary>
        public static DateTime GetChinaStandardTime()
        {
            TimeZoneInfo timezone = GetChinaStandardTimeTimeZone();
            DateTime datetime = TimeZoneInfo.ConvertTime(DateTime.Now, timezone);
            return datetime;
        }

        public static DateTime ToChinaStandardTime(this DateTime date)
        {
            TimeZoneInfo timezone = GetChinaStandardTimeTimeZone();
            DateTime datetime = TimeZoneInfo.ConvertTime(date, timezone);
            return datetime;
        }

        public static long TotalMilliseconds(this DateTime dateTime)
        {
            return (long)dateTime.Subtract(DateOf1970_01_01).TotalMilliseconds;
        }
        public static DateTime Parse(long totalMilliseconds)
        {
            return DateOf1970_01_01.AddMilliseconds(totalMilliseconds);
        }
        //获取相差年
        public static int GetYear(DateTime startTime, DateTime endTime)
        {
            int age = endTime.Year - startTime.Year;
            if (age > 0)
            {
                if (endTime.Month < startTime.Month)
                {
                    return age;
                }
                else if (endTime.Month == startTime.Month)
                {
                    if (endTime.Day > startTime.Day)
                    {
                        return age + 1;
                    }
                    else
                    {
                        return age;
                    }
                }
                else
                {
                    return age + 1;
                }

            }
            else if (age == 0)
            {
                return 1;
            }
            return 0;
        }

        //获取相差月
        public static int GetMonth(DateTime startTime, DateTime endTime)
        {
            int age = endTime.Year - startTime.Year;
            if (age > 0)
            {
                if (endTime.Month < startTime.Month)
                {
                    if (endTime.Day <= startTime.Day)
                    {
                        return age * 12 - startTime.Month + endTime.Month;
                    }
                    else
                    {
                        return age * 12 - startTime.Month + endTime.Month + 1;
                    }
                }
                else if (endTime.Month == startTime.Month)
                {
                    if (endTime.Day <= startTime.Day)
                    {
                        return age * 12;
                    }
                    else
                    {
                        return age * 12 + 1;
                    }

                }
                else
                {
                    if (endTime.Day <= startTime.Day)
                    {
                        return 12 * age + endTime.Month - startTime.Month;
                    }
                    else
                    {
                        return 12 * age + endTime.Month - startTime.Month + 1;
                    }
                }
            }
            else if (age == 0)
            {
                if (endTime.Month == startTime.Month)
                {
                    return 1;
                }
                else if (endTime.Month > startTime.Month)
                {

                    if (endTime.Day <= startTime.Day)
                    {
                        return endTime.Month - startTime.Month;
                    }
                    else
                    {
                        return endTime.Month - startTime.Month + 1;
                    }
                }
                return 0;
            }
            else
            {
                return 0;
            }

        }


    }
}
